package zy1212;

import org.apache.commons.io.FileUtils;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class demo {
    static List<student> slist = new ArrayList<>();
    static List<classes> clist = new ArrayList<>();

    public static void main(String[] args) throws Exception {
        init();
        for (classes c : clist) {
            for (student s : slist) {
                if (c.getClassID().equals(s.getClassID())){
                    c.setSlist(slist);
                }
            }
        }
        ageMax();
        mathmin();
        run();
        run1();
    }
    public static void run1() throws IOException {

        for (classes c : clist) {
            StringBuilder str = new StringBuilder();
            for (student s : slist) {
                if (c.getClassID().equals(s.getClassID())){
                    str.append(s.getNumber()+s.getName()+s.getAge()+s.getGender()+s.getMath()+s.getClassID()+"\r\n");
                }
            }
            FileUtils.writeStringToFile(new File(c.getClassname()+".txt"),str.toString());
        }
    }

    public static void run() {
        for (classes c : clist) {
            c.getSlist().sort(new Comparator<student>() {
                @Override
                public int compare(student o1, student o2) {
                    if (o1.getAge()==o2.getAge()){
                        return o2.getMath()-o1.getMath();
                    }else {
                        return o1.getAge()-o2.getAge();
                    }
                }
            });
            System.out.println(c.getSlist());
        }
    }

    public static void mathmin() {
        int index = 0, index1 = 0, pnum = 0, pmax = 0;
        double mathmin = 100;
        for (int i = 0; i < clist.size(); i++) {
            int sum = 0, num = 0,cnum = 0, index2 = 0;
            for (int j = 0; j < slist.size(); j++) {
                if (clist.get(i).getClassID().equals(slist.get(j).getClassID())) {
                    num++;
                    sum += slist.get(j).getMath();
                    if (slist.get(j).getMath() < 60) {
                        pnum++;
                    }
                    if (slist.get(j).getAge() < 18) {
                        cnum++;
                        index2 = i;
                    }
                }
            }
            if ((sum / num) < mathmin) {
                mathmin = 1.0 * sum / num;
                index = i;
            }
            if (pnum > pmax) {
                pmax = pnum;
                index1 = i;
            }
            System.out.println("班级名称:" + clist.get(index2).getClassname()
                    + " 未成年人数:" + cnum);
        }
        System.out.println("班级编号:" + clist.get(index).getClassID() + " 班级名称:" + clist.get(index).getClassname()
                + " 平均成绩:" + mathmin);
        System.out.println("班级编号:" + clist.get(index1).getClassID() + " 班级名称:" + clist.get(index1).getClassname()
                + " 不及格人数:" + pnum);

    }

    public static void ageMax() {
        int index = 0;
        double ageMax = 0;
        for (int i = 0; i < clist.size(); i++) {
            int sum = 0, num = 0;
            for (int j = 0; j < slist.size(); j++) {
                if (clist.get(i).getClassID().equals(slist.get(j).getClassID())) {
                    num++;
                    sum += slist.get(j).getAge();
                }
            }
            if ((sum / num) > ageMax) {
                ageMax = 1.0 * sum / num;
                index = i;
            }
        }
        System.out.println("班级编号:" + clist.get(index).getClassID() + " 班级名称:" + clist.get(index).getClassname()
                + " 讲师" + clist.get(index).getTeacher() + " 助理样式:" + clist.get(index).getAssistant() +
                " 辅导员:" + clist.get(index).getInstructor() + " 平均年龄:" + ageMax);
    }

    public static void init() throws Exception {
        SAXReader reader = new SAXReader();
        Document read = reader.read("D:\\ideadashuju12\\liu_ya_kai\\src\\zy1212\\student.xml");
        Element root = read.getRootElement();
        List<Element> list = root.elements();
        for (Element e : list) {
            slist.add(new student(e.attributeValue("number"), e.attributeValue("name"),
                    Integer.parseInt(e.attributeValue("age")), e.attributeValue("gender"),
                    Integer.parseInt(e.attributeValue("math")), e.attributeValue("classID")));
        }

        Document read1 = reader.read("D:\\ideadashuju12\\liu_ya_kai\\src\\zy1212\\classes.xml");
        Element root1 = read1.getRootElement();
        List<Element> list1 = root1.elements("classes");
        for (Element e : list1) {
            clist.add(new classes(e.attributeValue("classID"), e.attributeValue("classname"),
                    e.attributeValue("teacher"), e.attributeValue("assistant"), e.attributeValue("instructor")));

        }
    }
}
